package ynu;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Administrator extends User {

    public Administrator(String name, String id, String password, String type, String phone, String time) {
        super(name, id, password, type, phone, time);
    }

    public Administrator() {
    }

    //修改密码添加到日志
    public void changePasswordLog(String changetime,String name) throws IOException {
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\日志文件.txt", true));
        String message = "修改时间：" + changetime + ", 修改管理员" + name + "的密码, 操作人：" + name;
        writer1.write(message);
        writer1.newLine();
        writer1.close();
    }

    //用id查询用户信息
    public boolean idIterate(String path,String id) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while ((line = br.readLine()) != null){
            String[] mes = line.split("[,:]");
            if(mes.length>3){
                if (mes[3].equals(id)) {
                    System.out.println(line);
                    return true;
                }
            }
        }
        br.close();
        return false;
    }

    //用用户名查询用户信息
    public boolean nameIterate(String path,String name) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while ((line = br.readLine()) != null){
            String[] mes = line.split("[,:]");
            if(mes.length>1){
                if (mes[1].equals(name)) {
                    System.out.println(line);
                    return true;
                }
            }
        }
        br.close();
        return false;
    }

    //查询所有用户信息
    public void display() throws IOException {
        System.out.println("管理员：");
        printOut("D:\\java实训大作业\\管理员用户信息.txt");
        System.out.println("经理：");
        printOut("D:\\java实训大作业\\经理用户信息.txt");
        System.out.println("前台");
        printOut("D:\\java实训大作业\\前台用户信息.txt");
    }

    //添加管理员
    public void addAdministrator(String name, String id, String phone, String time, String addtime) throws IOException {
       if(duplicateCheck(id,name,phone,"D:\\java实训大作业\\前台用户信息.txt") && duplicateCheck(id,name,phone,"D:\\java实训大作业\\管理员用户信息.txt") && duplicateCheck(id,name,phone,"D:\\java实训大作业\\经理用户信息.txt")){
           BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\管理员用户信息.txt", true));
           String line = "用户名:" + name + ",用户id:" + id + ",手机号:" + phone + ",注册时间:" + time;
           writer.write(line);
           writer.newLine();
           writer.close();
           addLog(addtime,name,"张三","管理员");
           addIdPasswoed(id);
           System.out.println("添加成功！");
       }else{
           System.out.println("该用户名或id或电话号码已存在，添加失败");
       }
    }

    //添加经理
    public void addManager(String name, String id, String phone, String time, String addtime, String addname) throws IOException {
        if(duplicateCheck(id,name,phone,"D:\\java实训大作业\\前台用户信息.txt") && duplicateCheck(id,name,phone,"D:\\java实训大作业\\管理员用户信息.txt") && duplicateCheck(id,name,phone,"D:\\java实训大作业\\经理用户信息.txt")){
            BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\经理用户信息.txt", true));
            String line = "用户名:" + name + ",用户id:" + id + ",手机号:" + phone + ",注册时间:" + time;
            writer.write(line);
            writer.newLine();
            writer.close();
            addLog(addtime,name,addname,"经理");
            addIdPasswoed(id);
            System.out.println("添加成功！");
        }else{
            System.out.println("该用户名或id或电话号码已存在，添加失败");
        }
    }

    //添加前台
    public void addReception(String name, String id, String phone, String time, String addtime, String addname) throws IOException {
        if(duplicateCheck(id,name,phone,"D:\\java实训大作业\\前台用户信息.txt") && duplicateCheck(id,name,phone,"D:\\java实训大作业\\管理员用户信息.txt") && duplicateCheck(id,name,phone,"D:\\java实训大作业\\经理用户信息.txt")){
            BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\前台用户信息.txt", true));
            String line = "用户名:" + name + ",用户id:" + id + ",手机号:" + phone + ",注册时间:" + time;
            writer.write(line);
            writer.newLine();
            writer.close();
            addLog(addtime,name,addname,"前台");
            addIdPasswoed(id);
            System.out.println("添加成功！");
        }else{
            System.out.println("该用户名或id或电话号码已存在，添加失败");
        }
    }

    //遍历打印文件中的数据
    private void printOut(String path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while ((line = br.readLine()) != null){
            if(!line.equals("")){
                System.out.println(line);
            }
        }
        br.close();
    }

    //检查唯一性
    private boolean duplicateCheck(String id,String name,String phone,String path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while ((line = br.readLine()) != null) {
            String[] mes = line.split("[,:]");
            if(mes.length > 3){
                if (mes[3].equals(id)||mes[1].equals(name)||mes[5].equals(phone)) {
                    return false;
                }
            }
        }
        br.close();
        return true;
    }

    //添加到日志文件
    private void addLog(String addtime,String name,String addname,String type) throws IOException {
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\日志文件.txt", true));
        String message = "添加时间：" + addtime + ", 增加" +type + name + ", 操作人：" + addname;
        writer1.write(message);
        writer1.newLine();
        writer1.close();
    }

    //添加的id密码库
    private void addIdPasswoed(String id) throws IOException {
        BufferedWriter writer2 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\id密码库.txt", true));
        String idpassword = id + ",123456";
        writer2.write(idpassword);
        writer2.newLine();
        writer2.close();
    }

    //修改用户信息
    public void changeMessage() throws IOException {
        String id;
        String path;
        System.out.println("请选择修改用户类型：1.经理 2.前台（输入数字）");
        Scanner sc = new Scanner(System.in);
        int flag = sc.nextInt();
        switch (flag){
            case 1:
                String get = sc.nextLine();
                boolean label1;
                System.out.println("请输入要修改经理的id");
                do{
                    id = sc.nextLine();
                    if(judgeIdentity(id,"D:\\java实训大作业\\经理用户信息.txt")){
                        System.out.println("经理用户中没有此id,请重新输入：");
                        label1 = true;
                    }else {
                        label1 = false;
                    }
                }while(label1);
                System.out.println("请输入修改类型：1.修改用户类型 2.修改用户手机号（输入数字）");
                int flag1 = sc.nextInt();
                switch (flag1){
                    case 1:
                        String get2 = sc.nextLine();
                        System.out.println("请输入修改后的用户类型：1.管理员 2.前台（输入数字）");
                        int flag2 = sc.nextInt();
                        switch (flag2){
                            case 1:
                                BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\经理用户信息.txt"));
                                String line;
                                while ((line = br.readLine()) != null){
                                    String[] mes = line.split("[,:]");
                                    if(mes.length>3){
                                        if (mes[3].equals(id)) {
                                            BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\管理员用户信息.txt", true));
                                            writer.write(line);
                                            writer.newLine();
                                            writer.close();
                                        }
                                    }
                                }
                                br.close();
                                delete(id,"D:\\java实训大作业\\经理用户信息.txt");
                                System.out.println("修改成功！");
                                break;
                            case 2:
                                BufferedReader br1 = new BufferedReader(new FileReader("D:\\java实训大作业\\经理用户信息.txt"));
                                String line1;
                                while ((line1 = br1.readLine()) != null){
                                    String[] mes1 = line1.split("[,:]");
                                    if(mes1.length>3){
                                        if (mes1[3].equals(id)) {
                                            BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\前台用户信息.txt", true));
                                            writer1.write(line1);
                                            writer1.newLine();
                                            writer1.close();
                                        }
                                    }
                                }
                                br1.close();
                                delete(id,"D:\\java实训大作业\\经理用户信息.txt");
                                System.out.println("修改成功！");
                                break;
                        }
                        break;
                    case 2:
                        String number;
                        String get3 = sc.nextLine();
                        System.out.println("请输入修改后的电话号码");
                        number = sc.nextLine();
                        BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\经理用户信息.txt"));
                        String line;
                        ArrayList<String> list = new ArrayList<>();
                        while ((line = br.readLine()) != null) {
                            list.add(line);
                        }
                        br.close();
                        BufferedReader br1 = new BufferedReader(new FileReader("D:\\java实训大作业\\经理用户信息.txt"));
                        String line1 = "";
                        String line2 = "";
                        for(int i = 0; i < list.size(); i++){
                            line1 = br1.readLine();
                            String[] mes = line1.split("[:,]");
                            if(mes.length >= 7){
                                if(mes[3].equals(id)){
                                    mes[5] = number;
                                    line2 = mes[0] + ":" + mes[1] + "," + mes[2] + ":" + mes[3] + "," + mes[4] + ":" + mes[5] + "," + mes[6] + ":" + mes[7];
                                    list.set(i, line2);
                                }
                            }
                        }
                        br1.close();
                        BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\经理用户信息.txt"));
                        writer.write("");
                        writer.close();
                        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\经理用户信息.txt",true));
                        for(int i = 0; i < list.size(); i++){
                            writer1.write(list.get(i));
                            writer1.newLine();
                        }
                        writer1.close();
                        System.out.println("修改成功！");
                        break;
                }
                break;
            case 2:
                String get4 = sc.nextLine();
                boolean label2;
                System.out.println("请输入要修改前台的id");
                do{
                    id = sc.nextLine();
                    if(judgeIdentity(id,"D:\\java实训大作业\\前台用户信息.txt")){
                        System.out.println("前台用户中没有此id,请重新输入：");
                        label2 = true;
                    }else {
                        label2 = false;
                    }
                }while(label2);
                System.out.println("请输入修改类型：1.修改用户类型 2.修改用户手机号（输入数字）");
                int flag3 = sc.nextInt();
                switch (flag3){
                    case 1:
                        String get5 = sc.nextLine();
                        System.out.println("请输入修改后的用户类型：1.管理员 2.经理（输入数字）");
                        int flag4 = sc.nextInt();
                        switch (flag4){
                            case 1:
                                BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\前台用户信息.txt"));
                                String line;
                                while ((line = br.readLine()) != null){
                                    String[] mes = line.split("[,:]");
                                    if(mes.length>3){
                                        if (mes[3].equals(id)) {
                                            BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\管理员用户信息.txt", true));
                                            writer.write(line);
                                            writer.newLine();
                                            writer.close();
                                        }
                                    }
                                }
                                br.close();
                                delete(id,"D:\\java实训大作业\\前台用户信息.txt");
                                System.out.println("修改成功！");
                                break;
                            case 2:
                                BufferedReader br1 = new BufferedReader(new FileReader("D:\\java实训大作业\\前台用户信息.txt"));
                                String line1;
                                while ((line1 = br1.readLine()) != null){
                                    String[] mes1 = line1.split("[,:]");
                                    if(mes1.length>3){
                                        if (mes1[3].equals(id)) {
                                            BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\经理用户信息.txt", true));
                                            writer1.write(line1);
                                            writer1.newLine();
                                            writer1.close();
                                        }
                                    }
                                }
                                br1.close();
                                delete(id,"D:\\java实训大作业\\前台用户信息.txt");
                                System.out.println("修改成功！");
                                break;
                        }
                        break;
                    case 2:
                        String number;
                        String get6 = sc.nextLine();
                        System.out.println("请输入修改后的电话号码");
                        number = sc.nextLine();
                        BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\前台用户信息.txt"));
                        String line;
                        ArrayList<String> list = new ArrayList<>();
                        while ((line = br.readLine()) != null) {
                            list.add(line);
                        }
                        br.close();
                        BufferedReader br1 = new BufferedReader(new FileReader("D:\\java实训大作业\\前台用户信息.txt"));
                        String line1 = "";
                        String line2 = "";
                        for(int i = 0; i < list.size(); i++){
                            line1 = br1.readLine();
                            String[] mes = line1.split("[:,]");
                            if(mes.length >= 7){
                                if(mes[3].equals(id)){
                                    mes[5] = number;
                                    line2 = mes[0] + ":" + mes[1] + "," + mes[2] + ":" + mes[3] + "," + mes[4] + ":" + mes[5] + "," + mes[6] + ":" + mes[7];
                                    list.set(i, line2);
                                }
                            }
                        }
                        br1.close();
                        BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\前台用户信息.txt"));
                        writer.write("");
                        writer.close();
                        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\前台用户信息.txt",true));
                        for(int i = 0; i < list.size(); i++){
                            writer1.write(list.get(i));
                            writer1.newLine();
                        }
                        writer1.close();
                        System.out.println("修改成功！");
                        break;
                }
                break;
        }
    }

    //删除用户信息完整方法
    public void deleteMessage(String mainame) throws IOException {
        String id;
        String path;
        String deletetime;
        System.out.println("请选择删除用户类型：1.经理 2.前台 3.管理员（输入数字）");
        Scanner sc = new Scanner(System.in);
        int flag = sc.nextInt();
        String get = sc.nextLine();
        switch (flag){
            case 1:
                boolean label1;
                System.out.println("请输入要删除经理的id");
                do{
                    id = sc.nextLine();
                    if(judgeIdentity(id,"D:\\java实训大作业\\经理用户信息.txt")){
                        System.out.println("经理用户中没有此id,请重新输入：");
                        label1 = true;
                    }else {
                        label1 = false;
                    }
                }while(label1);
                System.out.println("请输入删除时间：");
                deletetime = sc.nextLine();
                System.out.println("是否确定删除：1.确定 2.取消（输入数字）");
                int flag1 = sc.nextInt();
                switch (flag1){
                    case 1:
                        deleteLog(deletetime,"经理",getName(id,"D:\\java实训大作业\\经理用户信息.txt"),getName(mainame,"D:\\java实训大作业\\管理员用户信息.txt"));
                        delete(id,"D:\\java实训大作业\\经理用户信息.txt");
                        deleteIdPassword(id);
                        System.out.println("删除成功！");
                        break;
                    case 2:
                        break;
                }
                break;
            case 2:
                boolean label2;
                System.out.println("请输入要删除前台的id");
                do{
                    id = sc.nextLine();
                    if(judgeIdentity(id,"D:\\java实训大作业\\前台用户信息.txt")){
                        System.out.println("前台用户中没有此id,请重新输入：");
                        label2 = true;
                    }else{
                        label2 = false;
                    }
                }while(label2);
                System.out.println("请输入删除时间：");
                deletetime = sc.nextLine();
                System.out.println("是否确定删除：1.确定 2.取消（输入数字）");
                int flag2 = sc.nextInt();
                switch (flag2){
                    case 1:
                        deleteLog(deletetime,"前台",getName(id,"D:\\java实训大作业\\前台用户信息.txt"),getName(mainame,"D:\\java实训大作业\\管理员用户信息.txt"));
                        delete(id,"D:\\java实训大作业\\前台用户信息.txt");
                        deleteIdPassword(id);
                        System.out.println("删除成功！");
                        break;
                    case 2:
                        break;
                }
                break;
            case 3:
                boolean label3;
                System.out.println("请输入要删除管理员的id");
                do{
                    id = sc.nextLine();
                    if(judgeIdentity(id,"D:\\java实训大作业\\管理员用户信息.txt")){
                        System.out.println("管理员用户中没有此id,请重新输入：");
                        label3 = true;
                    }else {
                        label3 = false;
                    }
                }while(label3);
                System.out.println("请输入删除时间：");
                deletetime = sc.nextLine();
                System.out.println("是否确定删除：1.确定 2.取消（输入数字）");
                int flag3 = sc.nextInt();
                switch (flag3){
                    case 1:
                        deleteLog(deletetime,"管理员",getName(id,"D:\\java实训大作业\\管理员用户信息.txt"),getName(mainame,"D:\\java实训大作业\\管理员用户信息.txt"));
                        delete(id,"D:\\java实训大作业\\管理员用户信息.txt");
                        deleteIdPassword(id);
                        System.out.println("删除成功！");
                        break;
                    case 2:
                        break;
                }
                break;
        }
    }

    //删除操作添加到日志
    private void deleteLog(String deletetime,String type,String name,String deletename) throws IOException {
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\日志文件.txt", true));
        String message = "删除时间：" + deletetime + ", 删除" + type + name + ", 操作人：" + deletename;
        writer1.write(message);
        writer1.newLine();
        writer1.close();
    }

    //单一删除
    private void delete(String id,String path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        ArrayList<String> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        BufferedReader br1 = new BufferedReader(new FileReader(path));
        String line1 = "";
        for(int i = 0; i < list.size(); i++){
            line1 = br1.readLine();
            String[] mes = line1.split("[:,]");
            if(mes.length>3){
                if(mes[3].equals(id)){
                    list.set(i,"");
                }
            }
        }
        br1.close();
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
        writer.write("");
        writer.close();
        BufferedWriter writer1 = new BufferedWriter(new FileWriter(path,true));
        for(int i = 0; i < list.size(); i++){
            writer1.write(list.get(i));
            writer1.newLine();
        }
        writer1.close();
    }

    //删除id密码库中的数据
    private void deleteIdPassword(String id) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\java实训大作业\\id密码库.txt"));
        String line;
        ArrayList<String> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        BufferedReader br1 = new BufferedReader(new FileReader("D:\\java实训大作业\\id密码库.txt"));
        String line1 = "";
        for(int i = 0; i < list.size(); i++){
            line1 = br1.readLine();
            String[] mes = line1.split("[:,]");
            if(mes.length>0){
                if(mes[0].equals(id)){
                    list.set(i,"");
                }
            }
        }
        br1.close();
        BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\java实训大作业\\id密码库.txt"));
        writer.write("");
        writer.close();
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\java实训大作业\\id密码库.txt",true));
        for(int i = 0; i < list.size(); i++){
            writer1.write(list.get(i));
            writer1.newLine();
        }
        writer1.close();
    }
}
